#!/usr/bin/python3
import os
import time
import shutil
import codecs

urlFile = "./urlFile.txt"
FileDir = "./recorder"
saveDir = "./result"       


def getUrlList(file):
    try:
        #print (results)
        listData = []
        with codecs.open(file, mode='r',encoding='utf-8') as fp:
            listData = fp.readlines()

    except:
        print("Error: unable to fetch data")
    return listData

def getDirFileList(dir):
    rootdir = os.path.abspath(dir)
    fileList = []
    for parent, dirnames, filenames in os.walk(rootdir):
        for filename in filenames:
            fileList.append(os.path.join(parent,filename))

    fileList.sort()
    print(fileList)
    return fileList

def main():
    toDir = os.path.abspath(saveDir)
    try:
        os.stat(toDir)
    except(OSError):
        os.mkdir(toDir)
    filelist = getDirFileList(FileDir)
    urlList = getUrlList(urlFile)

    findNum = 0
    moveNum = 0
    for file in filelist:
        name = os.path.basename(file)
        bfind = False
        for url in urlList:
            if (url.find(name) != -1):
                bfind = True
                break
        if(not bfind):
            findNum += 1
            try:
                shutil.move(file, toDir)
                moveNum += 1
                print("move file %s" %(name))
            except:
                print("move file %s failed" %(file))


    print("find file %d, move %d, failed %d" %(findNum, moveNum, findNum - moveNum))
    os.system('pause')



main()